//
//  AXFGoodsViewController.m
//  AIXF
//
//  Created by Macx on 2017/7/11.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "AXFGoodsViewController.h"
#import "AXFGoodsHeadView.h"
#import "AXFAccountManager.h"
#import "AXFHomeFirstSellModel.h"
#import "AXFGoodsTableViewCell.h"
#import "AXFSureBuyView.h"
#import <Masonry.h>
#import "AXFPayViewController.h"
#import "AXFAddressViewController.h"
#import "UILabel+Addition.h"
@interface AXFGoodsViewController ()<UITableViewDataSource>
//记录订单信息
@property (nonatomic,strong)NSMutableArray<AXFHomeFirstSellModel *> *modelArray;

@property (nonatomic,strong)UITableView * tableView;

@property (nonatomic,strong)AXFSureBuyView * sureView;

@property(nonatomic,strong)UIImageView *HomeImageView;
@property(nonatomic,strong)UILabel *textLabel;

@end

@implementation AXFGoodsViewController

- (NSMutableArray<AXFHomeFirstSellModel *> *)modelArray
{
    if (_modelArray == nil)
    {
        _modelArray = [NSMutableArray array];
    }
    return _modelArray;
}


//- (void)loadView
//{
//    [super loadView];
//    
//    if (_AXFAccountManager.goodsNum > 0)
//    {
//        [self loadData];
//        
//        [self setupUI];
//    }else
//    {
//        [self setupEmptyCar];
//    }
//    
//}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
   
    
    if (_AXFAccountManager.goodsNum > 0)
    {
        [self loadData];
        
        [self setupUI];
    }else
    {
        [self setupEmptyCar];
    }
    
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
//    if (_AXFAccountManager.goodsNum > 0)
//    {
//        [self loadData];
//        
//        [self setupUI];
//    }else
//    {
//        [self setupEmptyCar];
//    }
    
 
}

- (void)setupEmptyCar
{
    //设置导航栏的返回按钮
    [self.navigationController.navigationBar setBackgroundImage: [UIImage imageNamed:@""]forBarMetrics:UIBarMetricsDefault];
    //返回按钮
    UIBarButtonItem *disItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"v2_goback"] style:UIBarButtonItemStylePlain target:self action:@selector(dismiss)];
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    self.navigationItem.leftBarButtonItem = disItem;
    
    
    [self setupCarHome];
}

- (void)dismiss
{
    [self dismissViewControllerAnimated:YES completion:nil];
}


//空购物车,主界面
-(void)setupCarHome{
    
    //图片
    self.HomeImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"v2_shop_empty"]];
    [self.view addSubview:self.HomeImageView];
    [self.HomeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.width.offset(110);
        make.top.offset(200);
        make.centerX.equalTo(self.view);
    }];
    
    //字体
    self.textLabel = [UILabel makeLabelWithTextColor:[UIColor lightGrayColor] andTextWithFont:17 andText:@"亲,购物车空空的耶~赶紧挑选好吃的吧"];
    self.textLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.textLabel];
    [self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.view);
        make.top.equalTo(self.HomeImageView.mas_bottom).offset(20);
        make.centerX.equalTo(self.view);
    }];
    
    //去逛逛
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(100, 100, 150, 150);
    [btn setBackgroundImage:[UIImage imageNamed:@"btn.png"] forState:UIControlStateNormal];
    [btn setTitle:@"逛一逛" forState:UIControlStateNormal];
    [btn  setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    btn.titleLabel.textAlignment = NSTextAlignmentCenter;
    btn.titleLabel.font = [UIFont systemFontOfSize:17];
    [self.view addSubview:btn];
    
    //监听方法
    [btn addTarget:self action:@selector(ChilckButton) forControlEvents:UIControlEventTouchUpInside];
    
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.textLabel.mas_bottom).offset(30);
        make.width.offset(125);
        make.height.offset(40);
        make.centerX.equalTo(self.view);
        
    }];
    
}

-(void)ChilckButton{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
    
}


- (void)setupUI
{
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    
    _tableView = tableView;
    
    [tableView registerNib:[UINib nibWithNibName:@"AXFGoodsTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
    [self.view addSubview:tableView];
    
    AXFGoodsHeadView *goodHeadView = [AXFGoodsHeadView new];
    
    tableView.tableHeaderView = goodHeadView;
    
    for (UIView *subView in goodHeadView.subviews)
    {
        if ([subView.restorationIdentifier isEqualToString: @"1"])
        {
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickView)];
            [subView addGestureRecognizer:tap];
        }else if ([subView.restorationIdentifier isEqualToString: @"1000"])
        {
            //[];
        }
    }
    
    tableView.dataSource = self;
    
    tableView.tableFooterView = [UIView new];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveOrderNumChanged) name:kChangeOrderNumNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didSureNotification:) name:kSureBuyNotification object:nil];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"v2_goback"] style:UIBarButtonItemStylePlain target:self action:@selector(goback)];
    
    _sureView = [[AXFSureBuyView alloc] init];
    _sureView.totalPrice = _AXFAccountManager.allPrice;
    [self.view addSubview:_sureView];
    
    [_sureView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.height.offset(60);
    }];
}

- (void)clickView
{
    AXFAddressViewController *vc = [AXFAddressViewController new];
    
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)goback{
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)loadData
{
    self.modelArray = _AXFAccountManager.orderArray;

    [_tableView reloadData];
}

- (void)didReceiveOrderNumChanged
{
    self.modelArray = _AXFAccountManager.orderArray;
    
    _sureView.totalPrice = _AXFAccountManager.allPrice;
    
    [_tableView reloadData];
    
    if (_AXFAccountManager.goodsNum == 0)
    {
        [self.tableView removeFromSuperview];
        
        [_sureView removeFromSuperview];
        
        [self setupEmptyCar];
        
    }
}

- (void)didSureNotification: (NSNotification *)noti
{
    
    AXFPayViewController *vc = [[AXFPayViewController alloc] init];
    
    vc.modelArray = _AXFAccountManager.orderArray;
    
    [self.navigationController pushViewController:vc animated:YES];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.modelArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AXFGoodsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.model = self.modelArray[indexPath.row];
    
    
    return cell;
}


@end
